<?php

/**
 * 2017年6月6日14:40:50
 * PDO SAFE
 * LOW SPEED
 */

include 'common.php';

$username = $_COOKIE['username'];
$token = $_COOKIE['token'];
try {
//	$start = getRunTime();
//	print $start.'----';
	$dbc = pdo_root_connect();
//	$conntime = getRunTime();
//	print $conntime - $start.'----';
	$stmt = $dbc->prepare('select * from userinfo where username=? and token=? limit 1');
	$stmt->execute(array($username, $token));
	
	$wishlist = array();
	$msg = '';
	if($stmt->rowCount() > 0)
	{
		$stmt2 = $dbc->prepare('select * from userwish where username=? and status=0 order by uploadtime desc');
		$stmt2->execute(array($username));
		
		while($row = $stmt2->fetch()){
			
			$currentid = $row['id'];
			$stmt3 = $dbc->query("select * from dolikecheck where username='$username' and like2id='$currentid'");

			array_push($wishlist, ['id'=>(int)$currentid, 'type'=>(int)$row['hopetype'], 
					'time'=>(int)$row['uploadtime'], 'finishedtime'=>(int)$row['finishedtime'],
					'publishtime'=>(int)$row['publishtime'], 'good'=>(int)$row['good'],
					'is_mine'=>true, 'liked'=>(boolean)$stmt3->rowCount() > 0,
					'hope'=>$row['msg']]);
		}
		$errorcode = 1;
	}else{
		$errorcode = 3;
	}
	
//	print getRunTime() - $start;
}catch (PDOException $e){
	$msg = $e->getMessage();
	$errorcode = 4;
}
//	$result2 = mysql_query("select * from userwish where username='$username' and status='0' order by uploadtime desc");

//		for ($count = 0;$hoperow = mysql_fetch_array($result2);$count++)
// 		{
// 			$currenthopeid = $hoperow['id'];
// 			$hopelist[$count]->id = (int)$currenthopeid;
// 			$hopelist[$count]->hope = $hoperow['msg'];
// 			$hopelist[$count]->type = (int)$hoperow['hopetype'];
// 			$hopelist[$count]->time = (int)$hoperow['uploadtime'];
// 			$hopelist[$count]->finishedtime = (int)$hoperow['finishedtime'];
// 			$hopelist[$count]->publishtime = (int)$hoperow['publishtime'];
// 			$hopelist[$count]->good = (int)$hoperow['good'];
// 			$hopelist[$count]->is_mine = strcmp($hoperow['username'], $username) == 0;
// 			$liked = mysql_query("select * from dolikecheck where username='$username' and like2id='$currenthopeid'");
// 			$hopelist[$count]->liked = mysql_num_rows($liked);
// 		}
// 	}
// 	else
// 		$errorcode = 7;
// }else
// {
// 	$errorcode = 5;
// }

echo json_encode(array('errorcode'=>$errorcode, 'msg'=>$msg, 'wishlist'=>$wishlist));

?>